package cn.itcast.producer;

import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;
import org.apache.kafka.clients.consumer.ConsumerConfig;

import java.util.Properties;

/**
 * @author lilulu
 */
public class FlinkKafkaReader {
    public static void main(String[] args) throws Exception {
        // 1. 执行环境-env
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(3);
        // 2. 数据源-source
        Properties properties = new Properties();
        properties.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "node1:9092,node2:9092,node3:9092");
        properties.setProperty(ConsumerConfig.GROUP_ID_CONFIG, "_vehicle_consumer");
        FlinkKafkaConsumer<String> stringFlinkKafkaConsumer = new FlinkKafkaConsumer<String>(
                "vehicle_data",
                new SimpleStringSchema(),
                properties
        );
        stringFlinkKafkaConsumer.setStartFromLatest();
        DataStreamSource<String> dataStreamSource = env.addSource(stringFlinkKafkaConsumer);
        // 3. 数据转换-transformation
        // 4. 数据终端-sink
        dataStreamSource.printToErr("消费的数据>>>");
        // 5. 触发执行-execute
        env.execute("FlinkKafkaReader");
    }
}